<template>
   
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="searchPeopleList"
          size="small"
          label-width="140px"
        >
          <el-form-item label="邮箱:">
            <el-input
              style="width: 203px"
              v-model="searchPeopleList.email"
              placeholder="请输入要搜索的邮箱"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="姓名:">
            <el-input
              style="width: 203px"
              v-model="searchPeopleList.name"
              placeholder="请输入要搜索的姓名"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="性别:">
            <el-select
              v-model="searchPeopleList.sex"
              style="width: 203px"
              placeholder="请选择要搜索的性别"
              clearable
            >
              <el-option
                v-for="item in sexOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="账号状态:">
            <el-select
              v-model="searchPeopleList.status"
              style="width: 203px"
              placeholder="请选择要搜索的账号状态"
              clearable
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="联系方式:">
            <el-input
              style="width: 203px"
              v-model="searchPeopleList.phone"
              placeholder="请输入要搜索的联系方式"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card style="margin-top: 15px" class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div style="margin-top: 15px" class="table-container">
      <el-table
        :data="PeopleList"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="用户ID" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="邮箱" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.email }}</template>
        </el-table-column>
        <el-table-column label="姓名" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="联系方式" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="性别" width="100" align="center">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.sex == '-1'">保密</el-tag>
            <el-tag type="" v-else-if="scope.row.sex == '0'">男性</el-tag>
            <el-tag type="success" v-else-if="scope.row.sex == '1'"
              >女性</el-tag
            >
            <el-tag type="warning" v-else>其他</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="账号状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.status == '-1'">删除</el-tag>
            <el-tag type="warning" v-else-if="scope.row.status == '0'"
              >禁用</el-tag
            >
            <el-tag type="success" v-else>正常</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="创建时间" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="更新时间" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.updateTime }}</template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="
                (statusEditDialogVisible = true),
                  showStatusData(scope.$index, scope.row)
              "
              >状态修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="账号状态编辑"
        :visible.sync="statusEditDialogVisible"
        width="35%"
        :before-close="handleClose"
      >
        <span>
          <el-card class="filter-container" shadow="never">
            <div style="margin-top: 15px">
              <el-form
              :rules="editStatusRules"
                ref="editStatus"
                :inline="true"
                :model="editStatus"
                size="small"
                label-width="140px"
              >

                <el-form-item prop="status" label="账号状态:">
                  <el-select
                    v-model="editStatus.status"
                    style="width: 203px"
                    placeholder="请选择账号状态"
                    clearable
                  >
                    <el-option
                      v-for="item in statusOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-form>
            </div>
          </el-card>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="statusEditDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditStatus()"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        :page-count="pageCunt"
        :current-page.sync="currentPage"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "userManage",
  data() {
    return {
      uploadURL: "http://localhost:8080/api/upload/image",
      requestMethod: "POST",
      searchPeopleList: {
        name: "",
        type: "",
        status: "",
        phone: "",
        email: "",
        pageNum: "1",
        pageSize: "5",
      },
      currentPage:1,
      currentPeopleList: {
        name: "",
        type: "",
        status: "",
        phone: "",
        email: "",
        pageNum: "1",
        pageSize: "5",
      },
      PeopleList: [],
      editMessage: {
        name: "",
        type: "",
        status: "",
        phone: "",
        email: "",
      },
      editStatus: {
        id: "",
        status: "",
      },
      statusOptions: [
        {
          value: "0",
          label: "禁用",
        },
        {
          value: "-1",
          label: "删除",
        },
        {
          value: "1",
          label: "正常",
        },
      ],
      sexOptions: [
        {
          value: "1",
          label: "女性",
        },
        {
          value: "0",
          label: "男性",
        },
        {
          value: "2",
          label: "其他",
        },
        {
          value: "-1",
          label: "保密",
        },
      ],
      listLoading: true,
      isAdd: true,
      isEdit: true,
      addDialogVisible: false,
      messageEditDialogVisible: false,
      pageCunt: 1,
      statusEditDialogVisible: false,
      editStatusRules: {
        status: [{ required: true, message: "请输入账号ID", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      this.axios
        .get("api/auth/sys/user/listCustomer", {
          params: this.currentPeopleList,
        })
        .then((response) => {
          console.log(response)
          if (response.data.code == "200") {
            this.PeopleList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage=1
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleResetSearch() {
      this.searchPeopleList = {
        email: "",
        name: "",
        phone: "",
        sex: "",
        status: "",
        pageNum: "1",
        pageSize: "5",
      };
    },
    handleCurrentChange(val) {
      this.listLoading = true;
      this.currentPeopleList.pageNum = val;
      this.axios
        .get("api/auth/sys/user/listCustomer", {
          params: this.currentPeopleList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.PeopleList = response.data.value.data;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleSearchList() {
      this.listLoading = true;
      this.axios
        .get("api/auth/sys/user/listCustomer", {
          params: this.searchPeopleList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.PeopleList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage=1
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    showStatusData(index) { 
      this.editStatus = {...this.PeopleList[index]};
    },
    handleEditStatus() {
      this.$refs.editStatus.validate((valid) => {
        if (valid) {
          this.$confirm("请再次确认是否选择修改")
            .then((_) => {
              this.axios
                .post(
                  "api/auth/sys/user/updateCustomerStatus?id=" +
                    this.editStatus.id +
                    "&status=" +
                    this.editStatus.status
                )
                .then((response) => {
                  if (response.data.code == "200") {
                    this.$message({
                      message: "修改成功",
                      type: "success",
                    });
                    this.getList();
                    this.statusEditDialogVisible = false;
                  } else {
                    this.$message.error(response.data.message);
                  }
                })
                .catch((error) => {
                  this.$message.error("修改失败");
                });
            })
            .catch((_) => {});
        } else {
          this.$message.error("请填写完将带*号的选项");
        }
      });
    },
  },
};
</script>
<style></style>


